package cn.cdiothree.service.impl;

import cn.cdiothree.service.SendSmsCode;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
/**
 * 阿里云发送信息
 */
public class AliSendSmsCodeImpl implements SendSmsCode {
   @Override
   public void send(String telephone,String code){
       log.info("阿里云向手机号{}发送信息{}",telephone,code);
       DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FsptkbdDCVfNFNi2uQo", "6b111YwYEvb79bXYRC48gRSbMkJW3a");
       IAcsClient client = new DefaultAcsClient(profile);

       CommonRequest request = new CommonRequest();
       request.setMethod(MethodType.POST);
       request.setDomain("dysmsapi.aliyuncs.com");
       request.setVersion("2017-05-25");
       request.setAction("SendSms");
       request.putQueryParameter("RegionId", "cn-hangzhou");
       request.putQueryParameter("PhoneNumbers", telephone);
       request.putQueryParameter("SignName", "CUMThing周边");
       request.putQueryParameter("TemplateCode", "SMS_187226347");
       request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
       try {
           CommonResponse response = client.getCommonResponse(request);
           System.out.println(response.getData());
       } catch (ServerException e) {
           e.printStackTrace();
       } catch (ClientException e) {
           e.printStackTrace();
       }
   }
}
